<?php
/**
*
* @package phpBB3
* @version $Id: functions.php 8491 2008-04-04 11:41:58Z acydburn $
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

function generate_cryptlink($message, $post_data)
{
    global $db, $user, $phpEx, $phpbb_root_path, $auth, $config;
    $user->add_lang('mods/link');
    if(preg_match_all('~<a(| class="[^"]*") href="([a-z][a-z\d+\-.]*:/{2}[^"]*)"(| class="[^"]*")>([^<]*)</a>~U',$message, $return))
    {
        for($i=0; $i < sizeof($return['0']); $i++)
        {
            $url = $return['2'][$i];
            $txt =  $return['4'][$i];
            $sql = "SELECT id FROM " . LINK_TABLE . "
                        WHERE forum_id = '" . $post_data['forum_id'] . "'
                        AND post_id = '" . $post_data['post_id'] .  "'
                        AND url = '" . $db->sql_escape($url) . "'";
            $result = $db->sql_query_limit($sql, 1);
            if($db->sql_affectedrows($result) == 1 )
            {
                $row = $db->sql_fetchrow($result);
                $replace = $row['id'];
            }
            else
            {
                $random_id = substr(md5(rand(0,100) . mircotime),3, 6);
                $sql = "INSERT INTO " .  LINK_TABLE . "
                            (id, url, forum_id, post_id)
                            VALUES ('" . $random_id . "','" .  $db->sql_escape($url) . "','" .  $post_data['forum_id'] . "','" .  $post_data['post_id'] . "')";
                $db->sql_query($sql);
                $replace = $random_id;
            }
            if(hide_link() === true)
            {
                $message = str_replace($return['0'], $config['hide_link_text'], $message);
            }
            else
            {
                $message = str_replace('href="' . $url, 'href="' . append_sid("{$phpbb_root_path}link.php?link=$replace"), $message);
                if(!$auth->acl_get('u_see_real_link'))
                {
                    $repace = array('://', '.', ':', '/', '?', '=', '&');
                    $text_parts = explode(' ', strtolower(str_replace($repace, ' ', $txt)));
                    $domain_parts = explode(' ', strtolower(str_replace($repace, ' ', $url)));
                    if(sizeof(array_diff($domain_parts, $text_parts)) < (sizeof($domain_parts) * 0.76))
                    {
                        ($config['link_text']) ? $link_text = $config['link_text'] : $link_text = $user->lang['LINK_TEXT'];
                        if($config['link_add_id'])
                        {
                            (substr($link_text, strlen($link_text)-1) != ' ') ? $link_text .= ' ' : '';
                            $link_text .= $replace;
                        }
                        //Limited, only the Link...
                        $message = str_replace($txt . '</a>', $link_text . '</a>', $message);
                    }
                }
            }
        }
    }
    return $message;
}
/**
 * Because changes in phpBB3.0.2 a own function is needed
 *
 * @param string $url
 * @param int $time
 */
function do_redirect($url, $time)
{
    global $template, $user, $config;

    if($time == 0 || ($user->data['is_bot'] && $config['quick_bot_redirect']))
    {
        header('Location: ' .  $url);

    }
    else
    {
        $template->assign_vars(array(
            'META' => '<meta http-equiv="refresh" content="' . $time . ';url=' . $url . '" />')
        );
    }
    return true;
}
function insert_statistic($link_id)
{
    global $user, $config, $db;
    // Make statistik? Check it again...
    if($config['collect_link_data'])
    {
        $referer = $_SERVER['HTTP_REFERER'];
        $sql = 'INSERT INTO ' . LINK_LOG_TABLE . ' ' . $db->sql_build_array('INSERT', array(
    				'link_id'		=> (string) $db->sql_escape($link_id),
    				'user_id'		=> (int) $user->data['user_id'],
    				'referer'		=> (string)  $db->sql_escape($referer),
                    )
    			);
    	   $db->sql_query($sql);
    }
}
function hide_link()
{
    global $rowset, $user, $config;

    if(!$config['enable_hide_link'])
    {
        return false;
    }

    foreach ($rowset as $post_data)
    {
        if($post_data['user_id'] == ($user->data['user_id']))
        {
            return false;
        }
    }
    return true;
}
?>